#!/usr/bin/env bash


RUN_NAME=${TARGET_FILE_NAME}

run(){
    echo runing ./$RUN_NAME $2
    ./$RUN_NAME $2

}

start(){
    echo "start admin $2"
    nohup ./$RUN_NAME $2 > ../ac-sla.log 2>&1 &
    echo "runing $RUN_NAME $2"
}

stop(){

    PIDS=`ps -ef|grep $RUN_NAME |grep -v "grep"|awk '{print $2}'`

    echo "$PIDS"

    for pid in $PIDS
    do
        echo killing $pid
        kill -9 $pid


    done

}

restart(){
    stop && start $@
}

rerun(){
    stop && run $@
}


#

# Determine the script mode
action="$1"
if [ "$action" == "" ]
then
    action="run"
fi

# Call the appropriate action function
case "$action" in
    start)
      start "$@";;
    stop)
      stop "$@";;
    restart)
      restart "$@";;
    status)
      status "$@";;
    run)
      run "$@";;
    rerun)
      rerun "$@";;
    stop)
      stop "$@";;
    *)
  echo "Usage: $0 {start|stop|restart|run} {dev|test|vpcprod|...}";
  echo "    eg: ./adminconsole-sla_{GOOS}_{GOARCH}.run run dev";

  exit 1;
esac

exit 0